$(function () {
    $("#addBtn").bind("click", function () {
        window.location.href = root + "/role/input.do";
    });

    var ids = "";
    $("#deleteBtn").click(function () {
        ids = "";
        $('table tr > td:first-child input:checkbox').each(function () {
            if ($(this).prop("checked") === true) {
                ids += $.trim($(this).closest("tr").children('td').eq(1).html()) + ",";
            }
        });

        if (ids === "") {
            $().toastmessage('showToast', {
                text: "请选择要删除的角色！",
                stayTime: 3000,
                sticky: false,
                position: 'top-center',
                type: 'warning'
            });
            return;
        }

        ids = ids.substring(0, ids.length - 1);
        batchDel(ids, root + "/role/delete.do", root + "/role/list.do");
    });

    $(document).on('click', 'th input:checkbox', function () {
        var that = this;
        $(this).closest('table').find('tr > td:first-child input:checkbox').each(function () {
            // this.checked = that.checked;
            $(this).prop("checked", $(that).prop("checked"));
            var checked = $(this).prop("checked");
            if (checked === true)
                $(this).closest('tr').addClass('selected');
            else
                $(this).closest('tr').removeClass('selected');
        });
    });

    $('table tr > td:first-child input:checkbox').each(function () {
        var checked = $(this).prop("checked");
        if (checked === true)
            $(this).closest('tr').addClass('selected');
        else
            $(this).closest('tr').removeClass('selected');
    });

    $('tr > td:first-child input:checkbox').click(function () {
        var checked = $(this).prop("checked");
        if (checked === true)
            $(this).closest('tr').addClass('selected');
        else
            $(this).closest('tr').removeClass('selected');
    });

})

var List = (function () {
    return {
        editEntity: function ($this) {
            var id = $($this).parents("tr").children('td').eq(1).html();
            window.location.href = root + "/role/input.do?id=" + id;
        },
        deleteEntity: function ($this) {
            var id = $($this).parents("tr").children('td').eq(1).html();
            batchDel(id, root + "/role/delete.do", root + "/role/list.do");
        },
    }
})();
